package com.xzm.project.ui.activity;

import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.constraintlayout.widget.ConstraintSet;

import com.xzm.project.R;


/**
 * author : xuzongmeng
 * date   : 2019/8/21
 * desc   : 动态约束布局
 */
public class DynamicConstraintLayoutActivity extends AppCompatActivity {
    private LinearLayout rootView;
    private Button btnTest1;
    private Button btnTest2;
    private Button btnTest3;
    private Button btnTest4;
    private Button btnTest5;
    private Button btnTest6;

    public Context mContext;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mContext=this;
//        setContentView(R.layout.activity_dynamic_constraintlayout);
        setContentView(getLayoutRootView());
//        initView(savedInstanceState);



        initView();
//        initListener();
    }

    private void initView() {
        //第一个子布局容器
        ConstraintLayout constraintLayout = new ConstraintLayout(mContext);
        ConstraintLayout.LayoutParams layoutParams1 = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500);
        constraintLayout.setBackgroundColor(Color.RED);
        constraintLayout.setId(R.id.childContainer1);
        constraintLayout.setLayoutParams(layoutParams1);


        ConstraintSet constraintSet1=new ConstraintSet();
        btnTest1 = new Button(mContext);
        btnTest1.setId(R.id.btnTest1);
        btnTest1.setText("按钮1");
        constraintLayout.addView(btnTest1);
//        constraintSet.clone(constraintLayout);
        constraintSet1.constrainWidth(btnTest1.getId(), ViewGroup.LayoutParams.WRAP_CONTENT);
        constraintSet1.constrainHeight(btnTest1.getId(),ViewGroup.LayoutParams.WRAP_CONTENT);
        constraintSet1.connect(btnTest1.getId(),ConstraintSet.START, ConstraintSet.PARENT_ID,ConstraintSet.START);
        constraintSet1.connect(btnTest1.getId(),ConstraintSet.TOP, ConstraintSet.PARENT_ID,ConstraintSet.TOP);
//        constraintSet1.connect(btnTest1.getId(),ConstraintSet.END, ConstraintSet.PARENT_ID,ConstraintSet.END);
        constraintSet1.connect(btnTest1.getId(),ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM);
        constraintSet1.applyTo(constraintLayout);



        ConstraintSet constraintSet2=new ConstraintSet();
        btnTest2 = new Button(mContext);
        btnTest2.setId(R.id.btnTest2);
        btnTest2.setText("按钮2");
        constraintLayout.addView(btnTest2);
        constraintSet2.constrainWidth(btnTest2.getId(), ViewGroup.LayoutParams.WRAP_CONTENT);
        constraintSet2.constrainHeight(btnTest2.getId(),ViewGroup.LayoutParams.WRAP_CONTENT);
        constraintSet2.connect(btnTest2.getId(),ConstraintSet.START, R.id.btnTest1,ConstraintSet.END);
        constraintSet2.connect(btnTest2.getId(),ConstraintSet.TOP, R.id.btnTest1,ConstraintSet.TOP);
        constraintSet2.connect(btnTest2.getId(),ConstraintSet.END, R.id.btnTest3,ConstraintSet.START);
        constraintSet2.connect(btnTest2.getId(),ConstraintSet.BOTTOM, R.id.btnTest1,ConstraintSet.BOTTOM);
        constraintSet2.applyTo(constraintLayout);





        ConstraintSet constraintSet3=new ConstraintSet();
        btnTest3 = new Button(mContext);
        btnTest3.setId(R.id.btnTest3);
        btnTest3.setText("按钮3");
        constraintLayout.addView(btnTest3);
        constraintSet3.constrainWidth(btnTest3.getId(), ConstraintLayout.LayoutParams.WRAP_CONTENT);
        constraintSet3.constrainHeight(btnTest3.getId(),ConstraintLayout.LayoutParams.WRAP_CONTENT);
//        constraintSet1.connect(btnTest2.getId(),ConstraintSet.START, ConstraintSet.PARENT_ID,ConstraintSet.START);
        constraintSet3.connect(btnTest3.getId(),ConstraintSet.TOP, ConstraintSet.PARENT_ID,ConstraintSet.TOP);
        constraintSet3.connect(btnTest3.getId(),ConstraintSet.END, ConstraintSet.PARENT_ID,ConstraintSet.END);
        constraintSet3.connect(btnTest3.getId(),ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM);
        constraintSet3.applyTo(constraintLayout);




        ConstraintSet constraintSet4=new ConstraintSet();
        btnTest4 = new Button(mContext);
        btnTest4.setId(R.id.btnTest4);
        btnTest4.setText("按钮4");
        constraintLayout.addView(btnTest4);
        constraintSet4.constrainWidth(btnTest4.getId(), ConstraintLayout.LayoutParams.WRAP_CONTENT);
        constraintSet4.constrainHeight(btnTest4.getId(),ConstraintLayout.LayoutParams.WRAP_CONTENT);
        constraintSet1.connect(btnTest2.getId(),ConstraintSet.START,R.id.btnTest1,ConstraintSet.START);
        constraintSet4.connect(btnTest4.getId(),ConstraintSet.TOP,R.id.btnTest1,ConstraintSet.BOTTOM);
        constraintSet4.connect(btnTest4.getId(),ConstraintSet.END, R.id.btnTest1,ConstraintSet.END);
//        constraintSet4.connect(btnTest4.getId(),ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM);
        constraintSet4.applyTo(constraintLayout);

        rootView.addView(constraintLayout);
    }

    /**
     * 视图根容器  根容器必须设置 orientation
     */
    private View getLayoutRootView() {
        rootView = new LinearLayout(mContext);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ConstraintLayout.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        rootView.setOrientation(LinearLayout.VERTICAL);
        rootView.setLayoutParams(layoutParams);
        return rootView;
    }
    /**
     * 初始化Params
     */
    private ConstraintLayout.LayoutParams getConstraintParams() {
        ConstraintLayout.LayoutParams params = null;
        if (params == null) {
            params = new ConstraintLayout.LayoutParams(ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT);
        }
        return params;
    }


    protected void initView(Bundle savedInstanceState) {
        //第一个子布局容器
        ConstraintLayout constraintLayout1 = new ConstraintLayout(mContext);
        ConstraintLayout.LayoutParams layoutParams1 = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 500);
        constraintLayout1.setBackgroundColor(Color.RED);
        constraintLayout1.setId(R.id.childContainer1);
        constraintLayout1.setLayoutParams(layoutParams1);


        btnTest1 = new Button(mContext);
        btnTest1.setId(R.id.btnTest1);
        btnTest1.setText("按钮1");



        btnTest2 = new Button(mContext);
        btnTest2.setId(R.id.btnTest2);
        btnTest2.setText("按钮2");


        btnTest3 = new Button(mContext);
        btnTest3.setId(R.id.btnTest3);
        btnTest3.setText("按钮3");

        ConstraintLayout.LayoutParams constraintParams1 = getConstraintParams();
        ConstraintLayout.LayoutParams constraintParams2 = getConstraintParams();
        ConstraintLayout.LayoutParams constraintParams3 = getConstraintParams();

        ConstraintSet constraintSet1 = new ConstraintSet();
        ConstraintSet constraintSet2 = new ConstraintSet();
        ConstraintSet constraintSet3 = new ConstraintSet();
//        constraintSet1.connect(R.id.btnTest1,ConstraintSet.END,  ConstraintSet.PARENT_ID,ConstraintSet.END);

        constraintSet1.connect(btnTest1.getId(),ConstraintSet.END,  ConstraintSet.PARENT_ID,ConstraintSet.END);
        constraintSet1.connect(btnTest1.getId(),ConstraintSet.START, ConstraintSet.PARENT_ID,ConstraintSet.START);
        constraintSet1.connect(btnTest1.getId(),ConstraintSet.TOP, ConstraintSet.PARENT_ID,ConstraintSet.TOP);
        constraintSet1.connect(btnTest1.getId(),ConstraintSet.BOTTOM, ConstraintSet.PARENT_ID,ConstraintSet.BOTTOM);

        constraintSet1.applyTo(constraintLayout1);
//        constraintSet.
//                Tran
//        TransitionManager.
        btnTest1.setLayoutParams(constraintParams1);
        btnTest2.setLayoutParams(constraintParams2);
        btnTest3.setLayoutParams(constraintParams3);


//        constraintLayout1.addView(btnTest1);
//        constraintLayout1.addView(btnTest2);
//        constraintLayout1.addView(btnTest3);

        rootView.addView(constraintLayout1);

        //第二个子布局容器
        ConstraintLayout constraintLayout2 = new ConstraintLayout(mContext);
        ConstraintLayout.LayoutParams layoutParams2 = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        constraintLayout2.setBackgroundColor(Color.GREEN);
        constraintLayout2.setId(R.id.childContainer2);
        constraintLayout2.setLayoutParams(layoutParams2);




        btnTest4 = new Button(mContext);
        btnTest4.setId(R.id.btnTest4);
        btnTest4.setText("按钮4");

        btnTest5 = new Button(mContext);
        btnTest5.setId(R.id.btnTest5);
        btnTest5.setText("按钮5");

        btnTest6 = new Button(mContext);
        btnTest6.setId(R.id.btnTest6);
        btnTest6.setText("按钮6");



        ConstraintLayout.LayoutParams layoutParams4 = getConstraintParams();
        ConstraintLayout.LayoutParams layoutParams5 = getConstraintParams();
        ConstraintLayout.LayoutParams layoutParams6 = getConstraintParams();


        btnTest4.setLayoutParams(layoutParams4);
        btnTest5.setLayoutParams(layoutParams5);
        btnTest6.setLayoutParams(layoutParams6);

        constraintLayout2.addView(btnTest4);
        constraintLayout2.addView(btnTest5);
        constraintLayout2.addView(btnTest6);

        rootView.addView(constraintLayout2);

    }

    protected void initListener() {
        btnTest1.setOnClickListener(v -> Toast.makeText(mContext, "btnTest1", Toast.LENGTH_SHORT).show());
        btnTest2.setOnClickListener(v -> Toast.makeText(mContext, "btnTest2", Toast.LENGTH_SHORT).show());
        btnTest3.setOnClickListener(v -> Toast.makeText(mContext, "btnTest3", Toast.LENGTH_SHORT).show());
        btnTest4.setOnClickListener(v -> Toast.makeText(mContext, "btnTest4", Toast.LENGTH_SHORT).show());
        btnTest5.setOnClickListener(v -> Toast.makeText(mContext, "btnTest5", Toast.LENGTH_SHORT).show());
        btnTest6.setOnClickListener(v -> Toast.makeText(mContext, "btnTest6", Toast.LENGTH_SHORT).show());
    }
}
